<?php
	defined('RUN') or die('No access');

	class MemberController
	{
		public function __construct()
		{
			if (!Auth::check('acp'))
			{
				Url::redirect('admin/auth/login');
			}
		}
		
		public function index()
		{
			$data = Model::get('admin')->getLayoutData();
			$fields='`id`,`name`,`username`,`email`,`active`';
			$data['members'] = Model::get('member')->getMembers($fields);
			if (Session::getFlash('notification'))
			{
				$data['notification']=Session::getFlash('notification');
			}
			$data['content_file'] = 'member/view.tpl';
			View::load('layout.tpl', $data, 'admin');
		}

		public function add()
		{
			$data = Model::get('admin')->getLayoutData();

			if (Input::post('action') == 'add' && Security::checkToken('token_form', Input::post('token')))
			{
				if (Validator::checkRequired(Input::post(), array('txtName','txtUser','txtPass')))
				{
					if(Model::get('member')->checkUser(Input::post('txtUser')) == FALSE){
						$birth=explode(".",Input::post('txtBirth'));
						$birthday=$birth[2].'-'.$birth[0].'-'.$birth[1];
						$data = array(
							'name' 			=> strip_tags(Input::post('txtName')),
							'address' 		=> strip_tags(Input::post('txtAdd')),
							'birthday' 		=> $birthday,
							'sex'    		=> Input::post('chkSex'),
							'phone' 		=> strip_tags(Input::post('txtPhone')),
							'email' 		=> strip_tags(Input::post('txtEmail')),
							'username' 		=> strtolower(Input::post('txtUser')),
							'password' 		=> Security::hash(Input::post('txtPass')),
							'active'    	=> Input::post('chkActive'),
							'updated'    	=> date('Y-m-d h:m:s')							
						);
						
						if (Model::get('member')->add($data))
						{
							Session::setFlash('notification', array('type' => 'green', 'msg' => 'Thêm thành công !'));
							Url::redirect('admin/member');
						}
					}else{
						Session::setFlash('notification', array('type' => 'red', 'msg' => 'Username đã tồn tại !'));
						Url::redirect('admin/member');
					}
				}

				$data['failed'] = TRUE;
			}
                        
			$data['action'] = 'add';
			$data['content_file'] = 'member/form.tpl';
			View::load('layout.tpl', $data, 'admin');
		}
        
        public function update($id)
		{
			$data = Model::get('admin')->getLayoutData();
            $data['members'] = Model::get('member')->getById($id);

			if (Input::post('action') == 'update' && Security::checkToken('token_form', Input::post('token')))
			{
				if (Validator::checkRequired(Input::post(), array('txtName','txtUser')))
				{
						$password=Input::post('txtPass');
						if($password == '')
							$password = $data['members']['password'];
						else
							$password = Security::hash(Input::post('txtPass'));
						$birth=explode(".",Input::post('txtBirth'));
						$birthday=$birth[2].'-'.$birth[0].'-'.$birth[1];
						$data = array(
							'name' 			=> strip_tags(Input::post('txtName')),
							'address' 		=> strip_tags(Input::post('txtAdd')),
							'birthday' 		=> $birthday,
							'sex'    		=> Input::post('chkSex'),
							'phone' 		=> strip_tags(Input::post('txtPhone')),
							'email' 		=> strip_tags(Input::post('txtEmail')),
							'username' 		=> strtolower(Input::post('txtUser')),
							'password' 		=> $password,
							'active'    	=> Input::post('chkActive'),
							'updated'    	=> date('Y-m-d h:m:s')							
						);
	
						if (Model::get('member')->update($data, $id))
						{
							Session::setFlash('notification', array('type' => 'green', 'msg' => 'Chỉnh sửa thành công !'));
							Url::redirect('admin/member');
						}
				}

				$data['failed'] = TRUE;
			}
            
			$data['action'] = 'update';
			$data['content_file'] = 'member/form.tpl';
			View::load('layout.tpl', $data, 'admin');
		}                

		public function delete($id, $confirm = 'no')
		{
			$data = Model::get('admin')->getLayoutData();

			if ($confirm == 'yes')
			{     
                if (Model::get('member')->delete($id))
				{
					Session::setFlash('notification', array('type' => 'green', 'msg' => 'Xoá thành công !'));
					Url::redirect('admin/member');
				}
			}

			$data['members'] = Model::get('member')->getById($id);
			$data['content_file'] = 'member/delete.tpl';
			View::load('layout.tpl', $data, 'admin');
		}
	}

